package com.situ.mall.controller;



import com.situ.common.util.PageResult;
import com.situ.common.util.Result;
import com.situ.mall.constant.RedisConstants;
import com.situ.mall.pojo.entity.Category;
import com.situ.mall.pojo.entity.Product;
import com.situ.mall.pojo.query.ProductQuery;
import com.situ.mall.pojo.vo.ProductVO;
import com.situ.mall.service.ICategoryService;
import com.situ.mall.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private IProductService productService;
    @Autowired
    private ICategoryService categoryService;
    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/list")
    @ResponseBody
    public PageResult list(ProductQuery productQuery) {
        PageResult pageInfo = productService.list(productQuery);
        return pageInfo;
    }
    @RequestMapping("/updateStatus")
    @ResponseBody
    public Result updateStatus(Integer id, Integer status) {
        productService.updateStatus(id, status);
        return Result.ok("修改成功！");
    }
    @RequestMapping("/updateIsDeleted")
    @ResponseBody
    public Result updateIsDeleted(Product product) {
        productService.updateIsDeleted(product);
        return Result.ok("修改成功！");
    }
    @RequestMapping("/deleteById")
    @ResponseBody
    public Result deleteById(Integer id) {
        Integer judge = productService.judge(id);
        if (judge == 1) {
            productService.deleteById(id);
            return Result.ok("删除成功！");
        } else {
            return Result.error("删除失败，商品还存在于用户购物车和收藏等地方！");
        }

    }
    @RequestMapping("/deleteAll")
    @ResponseBody
    public Result deleteAll(Integer[] ids) {
        productService.deleteAll(ids);
        return Result.ok("删除成功！");
    }
    @RequestMapping("/add")
    @ResponseBody
    public Result add(Product product) {
        productService.add(product);
        //将Product中真正使用的图片保存到Redis中
        redisTemplate.opsForSet().add(RedisConstants.UPLOAD_IMAGE_TO_DB, product.getMainImage());
        return Result.ok("添加成功！");
    }
    @RequestMapping("/selectById")
    @ResponseBody
    public Result selectById(Integer id) {
        ProductVO productVO = productService.selectById(id);
        List<Category> listParent = categoryService.listAll();
        Integer parentId = productVO.getParentId();
        List<Category> listChild = categoryService.ByCategoryPatientId(parentId);
        Map<String, Object> map = new HashMap<>();
        map.put("productVO", productVO);
        map.put("listParent", listParent);
        map.put("listChild", listChild);
        return Result.ok(map);
    }
    @RequestMapping("/update")
    @ResponseBody
    public Result update(Product product) {

        productService.update(product);
        redisTemplate.opsForSet().add(RedisConstants.UPLOAD_IMAGE_TO_DB, product.getMainImage());
        return Result.ok("修改成功！");
    }
    @RequestMapping("/onAll")
    @ResponseBody
    public Result onAll(Integer[] ids) {
        productService.onAll(ids);
        return Result.ok("上架成功！");
    }
    @RequestMapping("/offAll")
    @ResponseBody
    public Result offAll(Integer[] ids) {
        productService.offAll(ids);
        return Result.ok("下架成功！");
    }
}
